package com.my.main.service.enterprise.serviceImpl;

import com.my.main.base.mapper.BaseMapper;
import com.my.main.base.service.Impl.BaseServiceImpl;
import com.my.main.common.utils.IdWorker;
import com.my.main.entity.enterprise.Enterprise;
import com.my.main.entity.enterprise.EnterpriseUserRelation;
import com.my.main.mapper.enterprise.EnterpriseMapper;
import com.my.main.mapper.enterprise.EnterpriseUserRelationMapper;
import com.my.main.service.enterprise.EnterpriseService;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class EnterpriseServiceImpl extends BaseServiceImpl<Enterprise> implements EnterpriseService {
    @Autowired
    private EnterpriseMapper enterpriseMapper;
    @Resource(name= "enterpriseMapper")
    public void setBaseMapper(BaseMapper<Enterprise> enterpriseMapper) {
        super.setBaseMapper(enterpriseMapper);
    }

    @Autowired
    IdWorker idWorker;
    @Autowired
    private EnterpriseUserRelationMapper enterpriseUserRelationMapper;

    @Override
    public void saveData(Map<String, Object> map, String ip, String userId) {
        Enterprise enterprise = new Enterprise();
        String enterpriseId = idWorker.nextId() +  "";
        Date date = new Date();
        enterprise.setEnterpriseId(enterpriseId);
        enterprise.setName(map.get("name").toString());
        enterprise.setAddress(StringUtils.isEmpty(map.get("address")) ? null : map.get("address").toString());
        enterprise.setContact(StringUtils.isEmpty(map.get("contact")) ? null : map.get("contact").toString());
        enterprise.setPhone(StringUtils.isEmpty(map.get("phone")) ? null : map.get("phone").toString());
        enterprise.setAddTime(date);
        enterprise.setAddIp(ip);
        enterprise.setAddUserId(userId);
        enterprise.setIsDel(false);
        enterpriseMapper.isSave(enterprise);
        if (!StringUtils.isEmpty(map.get("userIdList"))) {
            List<String> userIdList = (List<String>)map.get("userIdList");
            userIdList.stream().forEach(item -> {
                EnterpriseUserRelation eur = new EnterpriseUserRelation();
                eur.setRelationId(idWorker.nextId()+  "");
                eur.setUserId(item);
                eur.setEnterpriseId(enterpriseId);
                eur.setAddTime(date);
                eur.setAddIp(ip);
                eur.setAddUserId(userId);
                eur.setIsDel(false);
                enterpriseUserRelationMapper.isSave(eur);
            });
        }
    }

    @Override
    public void updateData(Map<String, Object> map, String ip, String userId) {
        Enterprise enterprise = enterpriseMapper.queryByStringId(map.get("enterpriseId").toString());
        enterprise.setName(map.get("name").toString());
        enterprise.setAddress(StringUtils.isEmpty(map.get("address")) ? null : map.get("address").toString());
        enterprise.setContact(StringUtils.isEmpty(map.get("contact")) ? null : map.get("contact").toString());
        enterprise.setPhone(StringUtils.isEmpty(map.get("phone")) ? null : map.get("phone").toString());
        enterpriseMapper.isUpdate(enterprise);
        Date date = new Date();
        Map<String, Object> paramMap = new HashedMap<>();
        paramMap.put("enterpriseId", enterprise.getEnterpriseId());
        enterpriseUserRelationMapper.isDeleteByParam(paramMap);
        if (!StringUtils.isEmpty(map.get("userIdList"))) {
            List<String> userIdList = (List<String>)map.get("userIdList");
            userIdList.stream().forEach(item -> {
                EnterpriseUserRelation eur = new EnterpriseUserRelation();
                eur.setRelationId(idWorker.nextId()+  "");
                eur.setUserId(item);
                eur.setEnterpriseId(enterprise.getEnterpriseId());
                eur.setAddTime(date);
                eur.setAddIp(ip);
                eur.setAddUserId(userId);
                eur.setIsDel(false);
                enterpriseUserRelationMapper.isSave(eur);
            });
        }
    }

    @Override
    public void deleteData(String enterpriseId) {
        enterpriseMapper.isDeleteByUuid(enterpriseId);
        Map<String, Object> paramMap = new HashedMap<>();
        paramMap.put("enterpriseId", enterpriseId);
        enterpriseUserRelationMapper.isDeleteByParam(paramMap);
    }
}
